﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;

namespace WeChat.Common2
{
    public class ReturnViewXml
    {
        public static string returnView(string ToUserName, string FromUserName, string createtime, string title, string description,string url)
        {
            XmlDocument doc = new XmlDocument();
            //MapPath命名空间默认page类，如果不在aspx.cs中使用需要使用server命名空间
            doc.Load(HttpContext.Current.Server.MapPath("ReturnView.xml"));
            //doc.ChildNodes[5].InnerText = 1;
            XmlNodeList xml = doc.DocumentElement.ChildNodes;
            foreach (XmlNode xn in xml)
            {
                switch (xn.Name)
                {
                    case "ToUserName":
                        xn.AppendChild(doc.CreateCDataSection(ToUserName));
                        continue;
                    case "FromUserName":
                        xn.AppendChild(doc.CreateCDataSection(FromUserName));
                        continue;
                    case "CreateTime":
                        xn.InnerText = DateTime.Now.Ticks.ToString();
                        continue;
                    case "MsgType"://消息类型
                        xn.AppendChild(doc.CreateCDataSection("news"));
                        continue;
                    case "ArticleCount"://图文消息个数，限制为10条以内
                        xn.AppendChild(doc.CreateCDataSection("1"));
                        continue;
                    case "Articles"://图文消息标题
                        XmlNodeList xm = xn.SelectSingleNode("item").ChildNodes;//查找子节点
                        foreach (XmlNode xd in xm)
                        {
                            switch (xd.Name)
                            {
                                case "Title":
                                    xd.AppendChild(doc.CreateCDataSection(title));
                                    continue;
                                case "Description":
                                    xd.AppendChild(doc.CreateCDataSection(description));
                                    continue;
                                case "PicUrl":
                                    xd.AppendChild(doc.CreateCDataSection("http://" + Config.domainName + "/WeChat/images/Recharge.jpg"));
                                    continue;
                                case "Url":
                                    xd.AppendChild(doc.CreateCDataSection(url));
                                    continue;
                                default:
                                    break;
                            }
                        }
                        continue;
                    default: 
                        break;
                }
                
            }
            //返回拼装好的xml字符串
            return doc.InnerXml;
        }
    }
}
